#include <cstdlib>
#include <cstdio>
#include <thread>

#define DR_WAV_IMPLEMENTATION

#include "dr_wav.h"
#include "src/dpl_log.h"
#include "src/dpl_error.h"
#include "src/audio_render.h"



using namespace dpl;

int main(int argc, char* argv[])
{
    int ret;
    drwav wav;

    if (!drwav_init_file(&wav, argv[1], NULL)) {
        // Error opening WAV file.
        fprintf(stderr, "failed to open wave file\n");
        goto error;
    }

    // TODO: fix the leak
    drwav_int16* pDecodedInterleavedPCMFrames = malloc(wav.totalPCMFrameCount * wav.channels * sizeof(drwav_int16));
    size_t numberOfSamplesActuallyDecoded = drwav_read_pcm_frames_s16(&wav, wav.totalPCMFrameCount, pDecodedInterleavedPCMFrames);

    // sdl init
    if(SDL_Init(SDL_INIT_AUDIO) < 0) {
        fprint(stderr, "failed to init sdl audio\n");
        goto error;
    }


    AudioRender render;
    AVCodecParameters param;

    param.sample_rate = wav.sampleRate;
    param.format = AV_SAMPLE_FMT_S16;
    param.channels = wav.channels;


    render.OpenAudio(param);
    
error:
    drwav_uninit(&wav);
}
